package com.bytecodezz.app.status

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import com.bytecodezz.app.R
import kotlinx.android.synthetic.main.activity_status_bar_main.*

class StatusBarMainActivity : AppCompatActivity() {

    var isImmersion = false
    var isPadding = false
    var isMargin = false

    var paddingText = "沉浸式状态栏paddingTop："
    var marginText = "沉浸式状态栏MarginTop："
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_status_bar_main)
        actionBar?.hide()
        supportActionBar?.hide()
        default_status.setOnClickListener {
            isImmersion = false
            StatusBarTools.setStatusBarColor(this, Color.parseColor("#398EFF"), false)
        }
        imm_status.setOnClickListener {
            isImmersion = true
            StatusBarTools.setStatusBarColor(
                this, Color.TRANSPARENT,
                true
            )
        }
        show_padding.setOnClickListener {
            isPadding = !isPadding
            StatusBarTools.setOffsetStatusBar(this, content_top_view, isPadding)
            show_padding.text = paddingText + isPadding
        }
        show_margin.setOnClickListener {
            isMargin = !isMargin
            StatusBarTools.setOffsetStatusBar(this, content_top_view, isMargin, false)
            show_margin.text = marginText + isMargin
        }
        status_bar_text_white.setOnClickListener {
            StatusBarTools.setStatusBarText(this@StatusBarMainActivity, Color.BLACK, isImmersion)
        }
        status_bar_text_dark.setOnClickListener {
            StatusBarTools.setStatusBarText(this@StatusBarMainActivity, Color.WHITE, isImmersion)
        }
        to_next.setOnClickListener {
            startActivity(Intent(this, StatusBarNextActivity::class.java))
        }

        initView()
    }

    /**
     * 初始化View
     */
    private fun initView() {
        sb_color.max = 255
        sb_color.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (isImmersion) {
                    if (progress == 0) {
                        StatusBarTools.setStatusBarColor(
                            this@StatusBarMainActivity,
                            Color.TRANSPARENT,
                            true
                        )
                        StatusBarTools.setStatusBarText(this@StatusBarMainActivity, Color.WHITE, true)
                    } else {
                        StatusBarTools.setStatusBarColor(
                            this@StatusBarMainActivity,
                            Color.argb(255, 255 - progress, 255 - progress, 255 - progress),
//                            Color.argb(progress, 255, 167, 141),
                            true
                        )
                    }
                } else {
                    if (progress == 0) {
                        StatusBarTools.setStatusBarColor(
                            this@StatusBarMainActivity,
                            Color.TRANSPARENT,
                            false
                        )
                        StatusBarTools.setStatusBarText(this@StatusBarMainActivity, Color.WHITE)
                    } else {
                        StatusBarTools.setStatusBarColor(
                            this@StatusBarMainActivity,
                            Color.argb(255, 255 - progress, 255 - progress, 255 - progress),
                            false
                        )
                    }
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })
    }
}